/**
 * 网络工具
 */
const os = require('os');
const getMac = require('getmac');

let mac = null;

module.exports = {
    /**
     * 获取IP地址。
     */
    getIpAdress() {
        const interfaces = os.networkInterfaces();
        for (let devName in interfaces) {
            let iface = interfaces[devName];
            for (let i = 0; i < iface.length; i++) {
                let alias = iface[i];
                if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
                    return alias.address;
                }
            }
        }

        return '';
    },

    /**
     * 获取网卡MAC地址.
     */
    getMacAddress() {
        return new Promise(function (resolve, reject) {
            if (mac) return resolve(mac);

            getMac.getMac(function (err, macAddress) {
                if (err) return reject(err);

                mac = macAddress;
                return resolve(macAddress);
            });
        });
    }
};